<?php

class MovimientosController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
     	 #$this->_helper->layout->disableLayout ();
		//$this->_helper->viewRenderer->setNoRender ();
		$this->_helper->viewRenderer->setNoRender ();
		$this->_helper->layout->disableLayout ();
		echo "<span>Movimientos</span><br>";
		echo $this->view->util()->getUser();
		 
    }
			
	public function movimientosAction(){
		 echo $this->view->util()->registerScriptJSController($this->getRequest());
		 /*.[MTipoTransac_list] 
			@pcod_tiptransac as varchar(5),
			@pdesc_tiptransac as varchar(150),
			@pflg_tipmovi as varchar(1),
			@pest_tiptransac varchar(1)*/
			//cbo_tiptrans
			$cn = new Model_Data();
			$parameter[]=array("@pcod_tiptransac",'');
			$parameter[]=array("@pdesc_tiptransac",'');
			$parameter[]=array("@pflg_tipmovi",'');
			$parameter[]=array("@pest_tiptransac",'');
			$result_bus = $cn ->ejec_store_procedura_sql("MTipoTransac_list", $parameter);
			for ($x=0;$x<count($result_bus);$x++)
    		$cboUM[] = array($result_bus[$x][0],$result_bus[$x][1],"{tipomovi:'".$result_bus[$x][2]."'}");
			
			$pintar = New Libreria_Pintar();
			$val[] = array('#cbo_tiptrans',$pintar->ContenidoCombo($cboUM,null),'html');
			$pintar->PintarValor($val);
	}
	
	public function buscarAction(){
		$this->_helper->getHelper ( 'ajaxContext' )->initContext ();
		
		if ($this->getRequest ()->isXmlHttpRequest ()) {
			$this->_helper->viewRenderer->setNoRender ();
			$this->_helper->layout->disableLayout ();
			
			$upper = new Zend_Filter_StringToUpper();
			$upper->setEncoding('UTF-8');
			$tip = $this->_request->getPost('tip');
			$val = $upper->filter($this->_request->getPost('val'));
			$chk = $this->_request->getPost('chk');
			$tiptrans =  $this->_request->getPost('tiptrans');
			$cn = new Model_Data();

			/*@pnum_mov as varchar(5),
			@pcod_tiptransac as varchar(150),
			@pdesc_razsoc varchar(150),
			@pfech_mov date=null,
			@pest_mov*/
			$parameter[]=array("@pcod_movi",'');
			$parameter[]=array("@pnum_mov",($tip=='0' ? $val:''));
			$parameter[]=array("@pcod_tiptransac",$tiptrans);
			$parameter[]=array("@pdesc_razsoc",($tip=='1' ? $val:''));
			if ($tip=='2') $parameter[]=array("@pfech_mov",$val);
			$parameter[]=array("@pest_mov",$chk);
			$result_bus = $cn ->ejec_store_procedura_sql("Mmovimientos_list", $parameter);
	

			if($result_bus==null || count($result_bus)==0){
				echo "Sin Resultados";
			}else{
				$tb="";
				foreach ($result_bus as $value) {
					$tb .= '<tr id="'.$value[0].'">';
					$tb .='<td>'.$value[1].'</td>';
					$tb .='<td>'.$value[3].'</td>';
					$tb .='<td>'.$value[7].'</td>';
					$tb .='<td>'.$value[5].'</td>';
					$tb .='<td>'.$value[10].'</td>';
					$tb .= '</tr>';
				}
				//echo '<table border="1"><tr><td>';
				echo '<table id="tbl_result" class="ui-widget ui-widget-content tablesorter">'.
						'<thead>'.
							'<tr class="ui-widget-header">'.
								'<th width="100px">Movimiento</th>'.
								'<th width="100px">Transaccion</th>'.
								'<th width="279px">Proveedor</th>'.
								'<th width="120px">Fecha</th>'.
								'<th width="120px">Monto</th>'.
							'</tr>'.
						'</thead>'.
						'<tbody>'.
							$tb.
						'</tbody>'.
					'</table>';

				//echo "</td></tr><tr><td>";
				echo $this->view->TableSorter()->ScriptDivPage();
				//echo $this->view->util()->registerScriptJSController($this->getRequest());
				//echo "</td></tr></table>";
				//$(\"#tbl_result\").tablesorter({sortList:[[0,0]], widgets: ['zebra'],headers: { 5:{sorter: false}}}).tablesorterPager({container: $(\"#pager\")});
				echo "<script type=\"text/javascript\">
						$(function() {		
							$(\"#tbl_result\").tablesorter({sortList:[[0,0]]})
							$(\"#tbl_result\").tablesorterPager({
								container: $(\"#pager\"),
								afterPagingCallBack: function(){
									AbrirMenu();
								}
							});
						});	
					</script>";
			}
			
		}
	}
	public function mantenimientoAction(){
		
     	echo $this->view->util()->registerScriptJSController($this->getRequest());
		$this->_helper->layout->disableLayout ();
		#$this->_helper->viewRenderer->setNoRender ();
		$type = $this->_request->getParam('type', '');
		$codMatePrima = $this->_request->getParam('codItem', '');
		$tiptrans = $this->_request->getParam('tiptrans', '');
		$parameter[]=array("@pcod_matpri",$codMatePrima);
		$parameter[]=array("@pdesc_nom",'');
		$parameter[]=array("@pest_matpri",'');
		$cn = new Model_Data();
		$pintar = New Libreria_Pintar();
	
		$CBparameter[]=array("@pcod_combin","");
		$CBparameter[]=array("@pdescripcion",'');
		$CBparameter[]=array("@pest_combin",'');//Poner en 1!!!

		$CBresult_bus = $cn ->ejec_store_procedura_sql("Mcombinaciones_list", $CBparameter);
		for ($x=0;$x<count($CBresult_bus);$x++)
    		$cboCB[] = array($CBresult_bus[$x][0],$CBresult_bus[$x][1]);
		
		if ($type=='N'){
			//$val[] = array('#cod_combin',$pintar->ContenidoCombo($cboCB,'null'),'html');	
			$val[] = array('#cod_combin','<option value="9999999999">Selecionar MP</option>','html');
		}elseif($type=='M' || $type=='E'){
			$result_bus = $cn ->ejec_store_procedura_sql("Mmateriaprima_list", $parameter);
			$val[] = array('#cboid_unidmed',$pintar->ContenidoCombo($cboUM,$result_bus[0][2]),'html');
			$val[] = array('#hdid_matprima',$result_bus[0][0],'val');
			$val[] = array('#txtds_nombre',$result_bus[0][1],'val');
			$val[] = array('#nu_stockmin',$result_bus[0][4],'val');
			
		}
		
		$pintar->PintarValor($val);
	
		echo '<script type="text/javascript">'.
						'$(function() {	'.
							'$("#tbl_detalleMovi").grider({'.
								'countRow : true,'.
								'countRowAdd : true,'.
								'afterPagingCallBack: function(){'.
									'callbackbusmatprima();'.
								'}'.
							"});".
						'});'.
		"</script>";
	}

	public function executemanteAction(){
		$this->_helper->getHelper ( 'ajaxContext' )->initContext ();
		
		if ($this->getRequest ()->isXmlHttpRequest ()) {
			$this->_helper->viewRenderer->setNoRender ();
			$this->_helper->layout->disableLayout ();
			
			$upper = new Zend_Filter_StringToUpper();
			$upper->setEncoding('UTF-8');
			$tip = $this->_request->getPost('type');
			$id_matpri = $upper->filter($this->_request->getPost('id_matpri'));
			$ds_nombre = $upper->filter($this->_request->getPost('ds_nombre'));
			$id_unidmed = $upper->filter($this->_request->getPost('id_unidmed'));
			$nu_stock = $upper->filter($this->_request->getPost('nu_stock'));
			$st_estado = $upper->filter($this->_request->getPost('st_stado'));
			
			$cn = new Model_Data();
			
			if($tip=='M' ||$tip=='N' ){
				$parameter[]=array("@ptype",$tip);
				$parameter[]=array("@pcod_matpri",$id_matpri);
				$parameter[]=array("@pdesc_nom",$ds_nombre);
				$parameter[]=array("@pcod_unidmed",$id_unidmed);
				$parameter[]=array("@pctd_stockmin",$nu_stock);
				$parameter[]=array("@pest_matpri",$st_estado);
				$parameter[]=array("@puser_reg",$this->view->util()->getUser());
				$parameter[]=array("@phost_reg",$this->view->util()->getHost());
				$result = $cn ->ejec_store_procedura_sql("Mmateriaprima_InsertUpdate", $parameter);
				 	
			}elseif($tip=='D'){
	
				$parameter[]=array("@pcod_matpri",$id_matpri);
				$parameter[]=array("@puser_reg",$this->view->util()->getUser());
				$parameter[]=array("@phost_reg",$this->view->util()->getHost());
				$result = $cn ->ejec_store_procedura_sql("Mmateriaprima_Delete", $parameter);
			}
			if($result[0][0]=='1') echo "Transaccion Exitosa";
			if($result[0][0]=='2') 
			 echo  $this->view->util()->getTextError($result[0]);
		}
		
	}
}